//
//  ViewController.m
//  RNDemo
//
//  Created by dafiger on 2020.04.17.
//  Copyright © 2020 dafiger. All rights reserved.
//

#import "ViewController.h"

#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>

@interface ViewController ()<RCTBridgeDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"开始测试。。。");
    [self performSelector:@selector(initRCTRootView) withObject:nil afterDelay:3.0f];
    // Do any additional setup after loading the view.
}

- (void)initRCTRootView {
    NSDictionary *dataDic = @{
        @"scores" : @[
                @{
                    @"name" : @"Alex",
                    @"value": @"42"
                },
                @{
                    @"name" : @"Joel",
                    @"value": @"10"
                }
        ]
    };
    
    RCTRootView *rootView = nil;
#ifdef DEBUG
    NSURL *jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.bundle?platform=ios&dev=true"];
    rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                           moduleName:@"RNDemo"
                                    initialProperties:dataDic
                                        launchOptions:nil];
#else
    RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self
                                              launchOptions:nil];
    rootView = [[RCTRootView alloc] initWithBridge:bridge
                                        moduleName:@"RNDemo"
                                 initialProperties:dataDic];
#endif
    
    rootView.backgroundColor = [UIColor blueColor];
    
    UIViewController *vc = [[UIViewController alloc] init];
    vc.view = rootView;
    [self presentViewController:vc animated:YES completion:nil];
}

#pragma mark - RCTBridgeDelegate
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
#if DEBUG
    return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
#else
    return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}

@end
